/**
 * FyLLGen - A Java based tool for collecting and distributing family data
 *
 * Copyright (C) 2007-2011 Christian Packenius
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.chris_soft.fyllgen.export;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;

import de.chris_soft.fyllgen.Statics;
import de.chris_soft.fyllgen.data.Person;
import de.chris_soft.fyllgen.data.Relationship;

/**
 * Exporting a plain text file.
 * @author Christian Packenius, 20110209.
 */
public class TextExporter extends DataExporter {
  /**
   * @see de.chris_soft.fyllgen.export.DataExporter#export(java.util.List,
   *      java.io.OutputStream)
   */
  @Override
  public long export(List<Person> persons, OutputStream outP) throws Exception {
    long documentNumber = DataExporter.createExportedFilesEntry(persons, this);

    PrintStream out = new PrintStream(outP);
    out.println("Liste aller Personen und deren Daten");
    out.println("====================================");

    int pID = 0;
    for (Person person : persons) {
      String name = person.getValueView(Person.NAME) + " [" + ++pID + "]";
      out.println();
      out.println(name);
      out.println(createString('-', name.length()));

      // Einzelne Personendaten ausgeben.
      boolean first = true;
      first = infoAusgeben(first, out, "Geburtstag:    ", person.getValueView(Person.BIRTHDAY), true);
      first = infoAusgeben(first, out, "geboren in:    ", person.getValueView(Person.BIRTHPLACE), false);
      first = infoAusgeben(first, out, "Todestag:      ", person.getValueView(Person.DEATHDAY), true);
      infoAusgeben(first, out, "gestorben in:  ", person.getValueView(Person.DEATHPLACE), false);

      infoAusgeben(true, out, "Beruf:         ", person.getValueView(Person.JOB), false);

      // Relationen zu anderen Personen ausgeben.
      relationAusgeben(out, Relationship.PARENTS, person, persons);
      relationAusgeben(out, Relationship.CHILDREN, person, persons);
      relationAusgeben(out, Relationship.PARTNERS, person, persons);
      relationAusgeben(out, Relationship.SIBLINGS, person, persons);

      out.println();
    }

    return documentNumber;
  }

  /**
   * Alle zu einer Relation passenden Personen ausgeben.
   */
  private void relationAusgeben(PrintStream out, String reltype, Person person, List<Person> persons) {
    Person[] rels = person.getRelationshipPersons(reltype);
    if (rels != null && rels.length > 0) {
      out.println();
      String title = getHeaderForPersonList(rels, reltype) + ":";
      out.println(title);
      for (Person p2 : rels) {
        out.println(" " + p2.getValueView(Person.NAME) + " [" + (persons.indexOf(p2) + 1) + "]");
      }
    }
  }

  /**
   * Gibt eine Zeile aus, falls die Daten vorhanden sind.
   * @param first
   */
  private boolean infoAusgeben(boolean first, PrintStream out, String title, String data, boolean b) {
    if (!b && data.length() == 0 || b && data.equals("?.?.?")) {
      // Nichts ausgeben.
    }
    else {
      if (first) {
        out.println();
      }
      first = false;
      if (b) {
        out.println(title + Statics.niceShortDate(data));
      }
      else {
        out.println(title + data);
      }
    }
    return first;
  }

  /**
   * Erzeugt einen String, der nur aus n gleichen Zeichen besteht.
   */
  private String createString(char c, int length) {
    StringBuffer sb = new StringBuffer(length);
    while (length-- > 0) {
      sb.append(c);
    }
    return sb.toString();
  }

  /**
   * @see de.chris_soft.fyllgen.export.DataExporter#getFileExtension()
   */
  @Override
  public String getFileExtension() {
    return "txt";
  }

  /**
   * @see de.chris_soft.fyllgen.export.DataExporter#getLongTitle()
   */
  @Override
  public String getLongTitle() {
    return "Text (plain text)";
  }

  /**
   * @see de.chris_soft.fyllgen.export.DataExporter#getShortTitle()
   */
  @Override
  public String getShortTitle() {
    return "Text";
  }
}
